package com.qingxin.wuye.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyBuilding;
import com.qingxin.wuye.entity.WyRoom;
import com.qingxin.wuye.entity.WyRoomFee;


@Service
public class WyRoomFeeService extends BaseService<WyRoomFee, String> {

	@Autowired
	private WyBuildingService buildingService;
	
	@Autowired
	private WyRoomService roomService;
	
	
	public Pager list(String gardenid, String buildingid, String roomid, String roomCode, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.entity.WyRoomFee(" +
				"r.feeid, r.roomCode, r.prjcode, r.prjname, r.price, r.isfee, r.cycle, r.cycleUnit, r.cycleNum," +
				" r.beginTime, r.endTime, r.accuracy, r.printIndex, r.feeArea, r.remark, r.latePaymentsRate, r.latePaymentsDays," +
				" r.incrementMonths, r.incrementPercent, r.tiered, r.feeMode, r.chargeMode, r.feeType, r.carCode," +
				" r.iscarport, r.feeEndTime, r.latestSerial, r.formulas, g.gardenName)" +
				" from WyRoomFee r, WyGarden g" +
				" where r.gardenid=g.gardenid ");
		
		if(StringUtils.isNotEmpty(gardenid) && !"0".equals(gardenid)){
			querier.append(" and r.gardenid=:gardenid ");
			querier.setParam("gardenid", gardenid);
		}
		
		if(StringUtils.isNotEmpty(buildingid) && !"0".equals(buildingid)){
			querier.append(" and r.buildingid=:buildingid ");
			querier.setParam("buildingid", buildingid);
		}
		
		if(StringUtils.isNotEmpty(roomid) && !"0".equals(roomid)){
			querier.append(" and r.roomid=:roomid ");
			querier.setParam("roomid", roomid);
		}
		
		if(StringUtils.isNotEmpty(roomCode)){
			roomCode = roomCode.replace("'", "");
			querier.append(" and r.roomCode like :roomCode ");
			querier.setParam("roomCode", "%"+roomCode+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加信息
	 * @param fee
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyRoomFee fee, LoginUser loginUser, HttpServletRequest request){
		WyRoom room = roomService.getWyRoomByRoomCode(fee.getBuildingid(), fee.getRoomCode());
		if(room==null){
			return new SysError("房间编号无效");
		}
		WyBuilding building = buildingService.get(fee.getBuildingid());
		if(building == null){
			return new SysError("参数无效");
		}
		fee.setRoomid(room.getRoomid());
		fee.setGardenid(building.getWyGarden().getGardenid());
		
		Date now = new Date();
		fee.setCreateTime(now);
		fee.setUpdateTime(now);
		fee.setCreateBy(loginUser.getLogin_name());
		fee.setUpdateBy(loginUser.getLogin_name());
		
		this.save(fee);
		
		return null;
	}
	
	
	/**
	 * 修改信息
	 * @param fee
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyRoomFee fee, LoginUser loginUser, HttpServletRequest request){
		WyRoomFee old = getSafeEntity(fee.getFeeid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		String fields = "itemid,prjcode,prjname,price,isfee,cycle,cycleUnit,cycleNum,beginTime,endTime," +
				"accuracy,printIndex,feeArea,remark,latePaymentsRate,latePaymentsDays,incrementMonths," +
				"incrementPercent,tiered,feeMode,chargeMode,feeType,carCode,iscarport,feeEndTime,latestSerial,formulas";
		BeanUtil.setTargetFields(old, fee, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyRoomFee> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除信息
	 * @param feeids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] feeids, LoginUser loginUser, HttpServletRequest request){
		for(String feeid : feeids){
			WyRoomFee fee = get(feeid);
			if(fee==null){
				continue;
			}
			
			this.delete(fee);
		}
		return null;
	}
	
}
